package com.sry.blog.sys.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sry.blog.sys.model.Tag;
import com.sry.blog.sys.mapper.TagMapper;
import com.sry.blog.sys.service.TagService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * InnoDB free: 10240 kB 服务实现类
 * </p>
 *
 * @author SR_Y
 * @since 2022-01-05
 */
@Service
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements TagService {

    /**
     * 创建和生成类型
     *
     * @param tag 类型
     * @return boolean
     */
    @Override
    public boolean creatOrUpdate(Tag tag) {
        return saveOrUpdate(tag);
    }

    /**
     * 获取类型信息
     *
     * @param tag 类型
     * @return 类型
     */
    @Override
    public Tag getOne(Tag tag) {
        final String tagName = tag.getTagName();
        QueryWrapper<Tag> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<Tag> lambda = wrapper.lambda();
        if (StrUtil.isNotEmpty(tagName)){
            lambda.eq(Tag::getTagName,tagName);
        }
        return getOne(wrapper);
    }

    /**
     * 获取类型列表
     *
     * @param tag 类型
     * @return 类型列表
     */
    @Override
    public List<Tag> list(Tag tag) {
        final String tagName = tag.getTagName();
        QueryWrapper<Tag> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<Tag> lambda = wrapper.lambda();
        if (StrUtil.isNotEmpty(tagName)){
            lambda.like(Tag::getTagName,tagName);
        }
        return list(wrapper);
    }

    /**
     * 删除类型
     *
     * @param id
     * @return
     */
    @Override
    public boolean delete(Integer id) {
        return removeById(id);
    }

    /**
     * 是否存在标签
     * @param tag
     * @return
     */
    @Override
    public boolean isExist(Tag tag) {
        Integer id = tag.getId();
        String tagName = tag.getTagName();
        QueryWrapper<Tag> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<Tag> lambda = wrapper.lambda();
        if(null!=id){
            return true;
        }
        return null!=getOne(tag);
    }
}
